package com.hfut.eeg.controller;

import com.hfut.eeg.common.api.CommonResult;
import com.hfut.eeg.dto.SystemUseTime;
import com.hfut.eeg.dto.TodayErrWaves;
import com.hfut.eeg.service.SysInfoService;
import com.hfut.eeg.service.SystemUserTimeService;
import com.hfut.eeg.service.tjf_DateBaoBiaoService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.sql.Date;
import java.util.List;
import java.util.Map;

/*
* @Auther tjf
* 设置系统使用时间和历史使用时间
* 获取病人每天平均检查时间和历史平均检查时间
* */

@Controller
@Api(tags = "SystemUseTimeController", value = "系统时间表")
@RequestMapping("/time")
public class SystemUseTimeController {

    @Autowired
    private TodayErrWaves todayErrWaves;
    @Autowired
    private SysInfoService sysInfoService;
    @Autowired
    private SystemUserTimeService systemUserTimeService;

    @Autowired
    private tjf_DateBaoBiaoService dateBaoBiaoService;


    /*
    * 按照日期年份
    * 前端传入的日期参数
    * 根据年份，获取识别率
    * 调用dateBaoBiaoService中的getRecognitionByYear方法
    * 返回一个list
    * */
    @ApiOperation("获取识别率")
    @RequestMapping(value = "/getRecognitionYear", method = RequestMethod.POST)
    @ResponseBody
    public CommonResult count3(Date date1, Date date2){
        if(date1 != null && date2 != null){
            List<Map<String, Object>> result = dateBaoBiaoService.getRecognitionByYear(date1,date2);
            if(result == null){
                return CommonResult.failed("获取病人信息失败");
            }else {
                return CommonResult.success(result);
            }
        }else {
            return CommonResult.failed("传入的日期为空！");
        }

    }

    /*
     * 按照日期月份
     * 前端传入的日期参数
     * 根据月份，获取识别率
     * 调用dateBaoBiaoService中的getRecognitionByMonth方法
     * 返回一个list
     * */
    @ApiOperation("获取识别率")
    @RequestMapping(value = "/getRecognitionMonth", method = RequestMethod.POST)
    @ResponseBody
    public CommonResult count4(Date date1, Date date2){
        if(date1 != null && date2 != null){
            List<Map<String, Object>> result = dateBaoBiaoService.getRecognitionByMonth(date1,date2);
            if(result == null){
                return CommonResult.failed("获取病人信息失败");
            }else {
                return CommonResult.success(result);
            }
        }else {
            return CommonResult.failed("传入的日期为空！");
        }

    }

    /*
     * 按照日期每天
     * 前端传入的日期参数
     * 根据天数，获取识别率
     * 调用dateBaoBiaoService中的getRecognitionByDay方法
     * 返回一个list
     * */
    @ApiOperation("获取识别率")
    @RequestMapping(value = "/getRecognitionDay", method = RequestMethod.POST)
    @ResponseBody
    public CommonResult count5(@RequestParam("date1") Date date1, @RequestParam("date2") Date date2){
        if(date1 != null && date2 != null){
            List<Map<String, Object>> result = dateBaoBiaoService.getRecognitionByDay(date1,date2);
            if(result == null){
                return CommonResult.failed("获取病人信息失败");
            }else {
                return CommonResult.success(result);
            }
        }else {
            return CommonResult.failed("传入的日期为空！");
        }

    }

    /*
    * 插入系统初始使用时间
    * */
    @RequestMapping(value = "/insertTime", method = RequestMethod.POST)
    @ResponseBody
    public void inserttime(String date){
        long l = systemUserTimeService.insertTime(date);
    }

    /*
    * 获取系统最开始投入使用的时间
    * */
    @RequestMapping(value = "/getInitTime", method = RequestMethod.POST)
    @ResponseBody
    public CommonResult getInitTime(String date){
        return systemUserTimeService.getInitTime();
    }

    /*
    * 更新系统当日开始使用时间
    *方便统计系统总共使用时间
    * */
    @RequestMapping(value = "/updateTime", method = RequestMethod.POST)
    @ResponseBody
    public CommonResult updatetime(){
        long l = systemUserTimeService.updateTime();
        return CommonResult.success("更新今日开始使用时间");
    }

    /*
    * 实时获取系统当前和历史使用时间
    * 返回一个map集合
    * */
    @RequestMapping(value = "/getTime", method = RequestMethod.POST)
    @ResponseBody
    public CommonResult gettime(){
        return systemUserTimeService.getSyetime();
    }

    /*
    * 计算系统关机前使用时间
    * 把今日累计使用时间存入数据库
    * */
    @RequestMapping(value = "/tadayUseTime", method = RequestMethod.POST)
    @ResponseBody
    public CommonResult historyTotalTime(){
        systemUserTimeService.totalSyetime();
        return CommonResult.success("今日累计使用时间存入数据库");

    }

    /*
    * 计算当天检查病人所花费的总时间求平均值
    * */
    @RequestMapping(value = "/dayPatientTime", method = RequestMethod.POST)
    @ResponseBody
    public CommonResult totaltime(Date date){
        CommonResult time = systemUserTimeService.getDayPatientTime(date);
        return time;
    }

    /*
    * 计算历史检查病人花费的总时间求平均值
    * 返回一个map集合
    * */
    @RequestMapping(value = "/historyPatientTime", method = RequestMethod.POST)
    @ResponseBody
    public CommonResult historytotaltime(){
        CommonResult time = systemUserTimeService.getHistoryPatientTime();
        return time;
    }

    /*
    * 计算今日检查过的病人的人数
    * 返回一个map集合
    * */
    @RequestMapping(value = "/getDayPatient", method = RequestMethod.POST)
    @ResponseBody
    public CommonResult getdaypatient(){
        CommonResult time = systemUserTimeService.getDayPatient();
        return time;
    }

    /*
    * 计算历史检查过的病人的人数
    * 返回一个map集合
    * */
    @RequestMapping(value = "/getHistoryPatient", method = RequestMethod.POST)
    @ResponseBody
    public CommonResult gethistorypatient(){
        CommonResult time = systemUserTimeService.getHistoryPatient();
        return time;
    }

    /*
    * 获取今日异常波数量
    * */
    @RequestMapping("/getTodayErrWaves")
    @ResponseBody
    public CommonResult getTodayErrWaves(){
        return CommonResult.success(todayErrWaves.getErrWaves());
    }

    /*
    * 获取系统总共异常波数量
    * */
    @RequestMapping("/getAllErrWaves")
    @ResponseBody
    public CommonResult getAllErrWaves(){
        return CommonResult.success(sysInfoService.getSysInfo().get("abWaveNum"));
    }

}
